/**
 * 
 */
package com.witframework.validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wuxinyang Sep 27, 2009 4:49:25 PM
 */
public class Validator {
	private HDIVValidations validations;

	public List editableParameterValuesValid(String url, String parameter, String[] values) {
		List messages=new ArrayList();
		Pattern p = null;
		Matcher m = null;
		Iterator userDefinedURLs = this.validations.getUrls().keySet().iterator();
		while (userDefinedURLs.hasNext()) {
			String regExp = (String) userDefinedURLs.next();
			p = Pattern.compile(regExp);
			m = p.matcher(url);
			if (m.matches()) {
				List userDefinedValidations = (List) this.validations.getUrls().get(regExp);
				for (int i = 0; i < userDefinedValidations.size(); ++i) {
					IValidation currentValidation = (IValidation) userDefinedValidations.get(i);
					if (!(currentValidation.validate(parameter, values))) {
						messages.add(currentValidation.message());						
					}
				}
			}
		}
		return messages;
	}

	public void setValidations(HDIVValidations validations) {
		this.validations = validations;
	}

	public boolean existValidations() {
		return ((this.validations != null) && (this.validations.getUrls() != null) && (this.validations.getUrls().size() > 0));
	}
}
